package com.qf.sl.youshare;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import com.qf.sl.youshare.adapters.CommonFragmentAdapter;
import com.qf.sl.youshare.fragments.SheJiaoCircleFragment;
import com.qf.sl.youshare.fragments.SheJiaoShareFragment;
import com.qf.sl.youshare.transform.ZoomOutPageTransformer;

import java.util.LinkedList;

/**
 * Created by IDEA
 * User : SL
 * on  2015/2/17.
 * 21:52
 */
public class SheJiaoActivity extends FragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shejiao);

        viewPager = (ViewPager) findViewById(R.id.shejiao_pager);

        ActionBar actionBar = getActionBar();

        if (actionBar != null) {

            initActionBar(actionBar);

        }

        LinkedList<Fragment> fragments = new LinkedList<Fragment>();
        fragments.add(new SheJiaoShareFragment());//添加分享Fragment
        fragments.add(new SheJiaoCircleFragment());//添加圈子Fragment
        CommonFragmentAdapter adapter =
                new CommonFragmentAdapter(
                        getSupportFragmentManager(),
                        fragments
                );
        viewPager.setAdapter(adapter);
        viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        viewPager.setOnPageChangeListener(this);

    }

    /**
     * 对ActionBar进行初始化操作
     * @param actionBar 要操作的ActionBar
     */
    private void initActionBar(ActionBar actionBar) {
        //设置ActionBar的 Tag 模式
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //设置返回按钮
        actionBar.setDisplayHomeAsUpEnabled(true);

        //去掉 ActionBar 上的图标
        actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

        //标题居中
        actionBar.setDisplayShowCustomEnabled(true);

        //从 layout 文件夹中加载布局,然后通过代码修改标题
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.actionbar_main_title, null);

        //获取 view 中的 TextView 进行标题的设置
        TextView textView = (TextView) view.findViewById(R.id.actionbar_title);
        if (textView != null) {
            textView.setText("社交");
        }

        actionBar.setCustomView(
                view,
                new ActionBar.LayoutParams(
                        Gravity.CENTER)
        );
        actionBar.setBackgroundDrawable(new ColorDrawable(0xFFA38F74));

        actionBar.setDisplayShowTitleEnabled(false);

        //增加Tag
        ActionBar.Tab tab = actionBar.newTab();

        tab.setText("分享");

        tab.setTag("1");
        tab.setTabListener(this);
        actionBar.addTab(tab);

        tab = actionBar.newTab();

        tab.setText("圈子");

        tab.setTag("2");
        tab.setTabListener(this);
        actionBar.addTab(tab);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.store, menu);

        return true;
    }

    //////////////////////// Android 中的 Tab 的点击事件 /////////////////////////

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        if (viewPager != null) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                int selectedIndex = actionBar.getSelectedNavigationIndex();
                viewPager.setCurrentItem(selectedIndex);
            }
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    /////////////////////// ViewPager 页面切换进行的事物处理 ////////////////////////

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setSelectedNavigationItem(i);
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}